package com.asset.dispoal.business.bsCase.service.commom;

import com.asset.dispoal.business.appoint.domain.db.mapper.AppointMapper;
import com.asset.dispoal.business.bsAgreement.domain.db.entity.AgreementCaseState;
import com.asset.dispoal.business.bsAgreement.domain.db.entity.AgreementStateRecord;
import com.asset.dispoal.business.bsAgreement.domain.db.mapper.AgreementMapper;
import com.asset.dispoal.business.bsCase.domain.db.mapper.BsCaseMapper;
import com.asset.dispoal.commom.menus.AgreementCaseSateMenu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tech.mhuang.pacebox.core.date.DateTimeUtil;
import tech.mhuang.pacebox.core.id.BaseIdeable;
import tech.mhuang.pacebox.core.util.StringUtil;
import tech.mhuang.pacebox.springboot.core.local.GlobalHeaderThreadLocal;
import tech.mhuang.pacebox.springboot.protocol.Result;

import java.util.ArrayList;
import java.util.List;

@Component
public class CaseAbolishCaseCommand {

    @Autowired
    private AgreementMapper agreementMapper;

    @Autowired
    private AppointMapper appointMapper;

    @Autowired
    private BsCaseMapper caseMapper;

    @Autowired
    private BaseIdeable<String> ideable;

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Result execute(String id, String wasteReasonId) {
        String userId = GlobalHeaderThreadLocal.getOrException().getUserId();
        List<AgreementCaseState> agreementCaseStateList = agreementMapper.queryAgreementCaseStateByCaseId(id);
        List<String> matchAgreementIdList = new ArrayList<>(agreementCaseStateList.size());
        for (AgreementCaseState agreementCaseState : agreementCaseStateList) {
            if (StringUtil.notEquals(AgreementCaseSateMenu.ABOLISH_CASE.getLabel(), agreementCaseState.getCaseState())) {
                matchAgreementIdList.add(agreementCaseState.getAgreementId());
                String oldState = agreementCaseState.getCaseState();
                agreementMapper.updateCaseStateAndReason(agreementCaseState.getAgreementId(), AgreementCaseSateMenu.ABOLISH_CASE.getLabel(), "1", userId, wasteReasonId);
                AgreementStateRecord agreementStateRecord = new AgreementStateRecord();
                agreementStateRecord.setId(ideable.generateId());
                agreementStateRecord.setContractId(agreementCaseState.getAgreementId());
                agreementStateRecord.setCreateUser(userId);
                agreementStateRecord.setCreateTime(DateTimeUtil.currentDate());
                agreementStateRecord.setOldState(oldState);
                agreementStateRecord.setNewState(AgreementCaseSateMenu.ABOLISH_CASE.getLabel());
                agreementMapper.insertStateRecord(agreementStateRecord);
            }
        }
        agreementMapper.deleteAgreementCaseByAgreementIds(matchAgreementIdList);
        //删除委派信息
        caseMapper.deleteRecordById(id);
        caseMapper.deleteById(id);
        appointMapper.deleteMapperByCaseId(id);
        return Result.ok();
    }
}
